package com.newbee.http;

import java.io.File;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import com.newbee.app.AppConfig;
import com.newbee.sound.Sound;

public class NewBeeHttpClient
{
	public static String EMAIL = "email";
	public static String NICK_NAME = "nickname";

	public static String HEAD = "head";

	public static String PASSWORD = "password";
	public static String FILE = "file";

	public static void SubmitPost(String email, String nickName, String head,
			String password)
	{

		HttpClient httpclient = new DefaultHttpClient();
		String acton = "userreg";
		HttpPost httppost = new HttpPost(AppConfig.HTTP + acton);
		try
		{

			FileBody img = new FileBody(new File(Sound.DIR + head));
			StringBody strEmail = new StringBody(email);
			StringBody strNickName = new StringBody(nickName);
			StringBody strPassWord = new StringBody(password);
			StringBody strHead = new StringBody(head);

			MultipartEntity reqEntity = new MultipartEntity();

			reqEntity.addPart(FILE, img);// file1为请求后台的File upload;属性
			reqEntity.addPart(EMAIL, strEmail);
			reqEntity.addPart(HEAD, strHead);
			reqEntity.addPart(NICK_NAME, strNickName);
			reqEntity.addPart(PASSWORD, strPassWord);
			reqEntity.addPart("username", new StringBody("王冲newbee"));

			httppost.setEntity(reqEntity);

			HttpResponse response = httpclient.execute(httppost);

			int statusCode = response.getStatusLine().getStatusCode();

			if (statusCode == HttpStatus.SC_OK)
			{

			}

		}
		catch (ParseException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			try
			{
				httpclient.getConnectionManager().shutdown();
			}
			catch (Exception ignore)
			{

			}
		}
	}

}
